<?php

namespace app\store\controller\appointment;

use app\common\enum\AppointmentStatus;
use app\store\controller\Controller;

use app\store\model\appointment\Config as ConfigModel;
use app\store\model\appointment\Time as TimeModel;
use app\store\model\appointment\Type as TypeModel;

/**
 * 预约配置
 * Class Recharge
 * @package app\store\controller\appointment
 */
class Config extends Controller
{
    /**
     * 会员等级列表
     * @return mixed
     * @throws \think\exception\DbException
     */
    public function index()
    {
        $model = new ConfigModel;
        $list = $model->getListByPage();
        return $this->fetch('index', compact('list'));
    }

    /**
     * 添加等级
     * @return array|bool|mixed
     * @throws \Exception
     */
    public function add()
    {
        $model = new ConfigModel;
        if (!$this->request->isAjax()) {

            $status_list = AppointmentStatus::data();

            $timeModel = new TimeModel;
            $time_list = $timeModel->getList();

            $typeModel = new TypeModel;
            $type_list = $typeModel->getList();

            return $this->fetch('add',
                array_merge(compact('status_list', 'time_list', 'type_list')));

            return $this->fetch('add');
        }

        // 新增记录
        if ($model->add($this->postData('config'))) {
            return $this->renderSuccess('添加成功', url('appointment.config/index'));
        }
        return $this->renderError($model->getError() ?: '添加失败');
    }

    /**
     * @param $type_id
     * @return array|bool
     */
    public function delete($config_id)
    {
        // 会员等级详情
        $model = ConfigModel::detail($config_id);
        if (!$model->setDelete()) {
            return $this->renderError($model->getError() ?: '删除失败');
        }
        return $this->renderSuccess('删除成功');
    }
}